package com.service.user.impl;

import com.dao.NoticeDao;
import com.entity.Notice;
import com.service.user.NoticeService;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;

/**  */
@Service
public class NoticeServiceImpl implements NoticeService {

  @Override
  public List<Notice> getAllNotices(
      String userId, Integer status, Integer pageSize, Integer pageIndex) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    NoticeDao dao = session.getMapper(NoticeDao.class);
    Integer start = (pageSize == null ? null : pageSize * pageIndex);
    List<Notice> notices = dao.selectUserNotice(userId, status, start, pageSize);
    session.close();
    for (Notice notice : notices) {
      if (notice.getTemplate() == null) {
        notice.setTemplate(Notice.getDefaultTemplate(notice.getKind()));
      }
    }
    return notices;
  }

  @Override
  public int addNotice(String userId, int kind, String template, int status, Object... params) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    NoticeDao dao = session.getMapper(NoticeDao.class);
    int row = insertHelper(dao, userId, kind, template, status, params);
    session.commit();
    session.close();
    return row;
  }
}
